<?php
/**
 * Created by PhpStorm.
 * User: qingyun
 * Date: 17/5/9
 * Time: 下午9:00
 */
namespace Home\Controller;
use Think\Controller;
class CollectionController extends Controller{
    public function index(){
        $pid = I('get.pid/d',0);
        $cates = I('get.cates/d',0);
        $page = I('get.page/d',1);
        //判断是想获取大类还是小类的藏品列表
        if(!empty($pid)){
            $wheresql = 'pid=' .$pid;
        }elseif(empty($cates)){
            $wheresql = '';
        }else{
            $wheresql = 'cates=' .$cates;
        }
        //右侧导航栏
        $list = M('columns') -> field('id,pid,columns') ->where(['type' => '2'])-> select();
        $list = make_tree($list);
        //分页
        $collection = M('collection');
        $count = $collection ->where($wheresql) ->count();
        $limit = 1;
        $pages =ceil($count/$limit);
        $pageNum = ($page -1) * $limit;
        $data = $collection -> field('id,name,litpic') -> where($wheresql)->limit($pageNum,$limit) -> select();//获取藏品列表数据
        $arr = [];//只读取一张图片//读出藏品列表详情
        foreach ($data as $key => $val) {
            $arr[$key]['id'] =$val['id'];
            $arr[$key]['name'] =$val['name'];
            $arr[$key]['pic'] =reset(explode(',',$val['litpic']));
        }
        $data1 = $collection -> field('id,name,litpic') ->limit(4)->order('id desc')-> select();//获取藏品列表数据
        $arr1 = [];//只读取一张图片//读出藏品列表详情
        foreach ($data1 as $key => $val) {
            $arr1[$key]['id'] =$val['id'];
            $arr1[$key]['name'] =$val['name'];
            $arr1[$key]['pic'] =reset(explode(',',$val['litpic']));
        }
        $configArr = [];
        $configInfo = M('Config') -> field('title,value,variable') -> select();
        foreach($configInfo as $key => $val){
            $configArr[$val['variable']] = $val['value'];
        }
        $info = $collection -> field('cates,columns')->join('left join dx_columns c on c.id = dx_collection.cates') ->where('cates = '. $cates) ->select();
        //读取
        $this ->assign('info',$info);
        $this ->assign('nav11',$cates);
        $this->assign('configArr',$configArr);
        $this ->assign('arr',$arr1);
        $this ->assign('pages',$pages);
        $this ->assign('lists',$arr);
        $this ->assign('nav',$list['0']['_child']);
        $this -> display();
    }
    public function detail(){
        $id = I('get.id/d',0);
        //右侧导航栏
        $list = M('columns') -> field('id,pid,columns') ->where(['type' => '2'])-> select();
        $list = make_tree($list);
        $this ->assign('nav',$list['0']['_child']);
        //根据id获取藏品详情信息
        $collection = M('collection');
        $info = $collection -> field('dx_collection.pid,name,number,author,material,size,weight,pubdate,litpic,content,columns,keywords') -> join('left join dx_columns c on c.id = dx_collection.pid ') -> where('dx_collection.id=' .$id) -> find();
        //将存储为字符串的多张缩略图路径打散为数组方便读取，选取四个
        $litpic = explode(',',trim($info['litpic'],','));
        if(count($litpic)>=4){
            $litpic = array_slice($litpic,0,4);
        }
        $data = $collection -> field('id,name,litpic') ->limit(5)->order('id desc')-> select();//获取藏品列表数据
        $arr1 = [];//只读取一张图片//读出藏品列表详情
        foreach ($data as $key => $val) {
            $arr1[$key]['id'] =$val['id'];
            $arr1[$key]['name'] =$val['name'];
            $arr1[$key]['pic'] =reset(explode(',',$val['litpic']));
        }
        $this ->assign('arr',$arr1);
        array_pop($arr1);
        $before = $collection ->field('id,name')-> where('id<' .$id) -> limit(1)->order('id desc')-> find();
        $next = $collection ->field('id,name')-> where('id>' .$id) -> limit(1)->order('id asc')-> find();
        $configArr = [];
        $configInfo = M('Config') -> field('title,value,variable') -> select();
        foreach($configInfo as $key => $val){
            $configArr[$val['variable']] = $val['value'];
        }
        $info2 = $collection -> field('cates,columns') ->join('left join dx_columns c on c.id = cates')->where('dx_collection.id ='.$id)->find();
        //读取
        $this->assign('configArr',$configArr);
        $this ->assign('before',$before);
        $this ->assign('next',$next);
        $this ->assign('array',$arr1);
        $this ->assign('info',$info);
        $this ->assign('info2',$info2);
        $this ->assign('litpic',$litpic);
        $this -> display();
    }
}